﻿function AzureTableQueryCompareExpressionModel(parentExpr, builderContext) {

    var self = this;

    var parent = ko.observable(parentExpr);

    var left = ko.observable();

    var right = ko.observable();

    var operator = ko.observable();

    var edmType = ko.observable('');
    
    var canRemove = ko.computed(function () {
        return false;
    });

    var canAdd = ko.computed(function () {
        return true;
    });

    function removeChild() {
    }

    function addExpression() {
        var me = new AzureTableQueryCombineExpressionModel(parent, builderContext);
        parent().replaceChild(self, me);
        me.addChild(self);
        me.addChild(new AzureTableQueryCompareExpressionModel(me, builderContext));
    }

    function invert() {
        var res = new AzureTableQueryFuncExpressionModel(parent(), builderContext);
        res.funcName('not');
        res.arg(self);
        parent().replaceChild(self, res);
    }

    function highlight() {
        builderContext.highlightedExpression(self);
    }

    self.type = 'compare';

    var formattedValue = ko.computed(function () {
        var val = right();
        switch (edmType()) {
            case 'Edm.Boolean':
                return !!val ? 'true' : 'false';
            case 'Edm.Double':
            case 'Edm.Int32':
            case 'Edm.Int64':
                return val;
            case 'Edm.DateTime':
                return 'datetime\'' + val + '\'';
            case 'Edm.Guid':
                return 'guid\'' + val + '\'';
            case 'Edm.Binary':
                return 'X\'' + val + '\'';
            case '':
            case 'Edm.String':
                return "'" + val + "'";
            default:
                return "'" + val + "'";
        }
    });
    
    var queryString = ko.computed(function () {
        var l = left() || '<colname>';
        var o = operator() || '<operator>';
        var r = formattedValue() || '<value>';
        var res = '';
        res += l;
        res += ' ';
        res += o;
        res += ' ';
        res += r;
        return res;
    });

    var queryHtmlString = ko.computed(function () {
        var l = left();
        if (l) {
            l = '<span class="col-name">' + l + '</span>';
        } else {
            l = '<span class="missing-value">&lt;colname&gt;</span>';
        }
        var o = operator() || '<span class="missing-value">&lt;operator&gt;</span>';
        var r = formattedValue();
        if (r) {
            r = '<span class="col-value">' + r + '</span>';
        } else {
            r = '<span class="missing-value">&lt;value&gt;</span>';
        }
        var res = '';
        res += l;
        res += ' ';
        res += o;
        res += ' ';
        res += r;
        if (builderContext.highlightedExpression() == self) {
            res = '<span class="highlighted">' + res + '</span>';
        }
        return res;
    });

    self.parent = parent;
    self.left = left;
    self.operator = operator;
    self.right = right;
    self.edmType = edmType;
    self.canAdd = canAdd;
    self.canRemove = canRemove;
    self.invert = invert;
    self.removeExpr = removeChild;
    self.addExpression = addExpression;
    self.highlight = highlight;
    self.queryString = queryString;
    self.queryHtmlString = queryHtmlString;
}